/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package evidence.model.modeloveTridy;

import evidence.model.pomocneTridy.Den;
import evidence.model.pomocneTridy.Time;
import java.awt.Color;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;

/**
 *
 * @author Protheo
 */
public class MesicniEvidence implements Serializable {

    public static final long serialVersionUID = 1;
    private final int[] dnu = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    private static String[] mesice = {"Leden", "Únor", "Březen", "Duben", "Květen", "Červen",
        "Červenec", "Srpen", "Září", "Řájen", "Listopad", "Prosinec"};
    private final Integer rok;
    private final Integer mesic;
    private List<DenEvidence> dny;
    private Map<String, Double> hodiny;
    private Map<String, Integer> doprava;
    private Double celkHodiny;
    private Integer celkDoprava;
    private Den den;

    public MesicniEvidence(Integer rok, Integer mesic) {
        this.rok = rok;
        this.mesic = mesic;
        this.init();
    }

    private void init() {
        if (this.mesic == 2 && this.jePrestupny(this.rok)) {
            dny = new ArrayList<DenEvidence>(29);
        } else {
            dny = new ArrayList<DenEvidence>(dnu[this.mesic - 1]);
        }
        this.hodiny = new HashMap<String, Double>(TypSluzby.pocetSluzeb());
        this.doprava = new HashMap<String, Integer>(TypSluzby.pocetSluzeb());
        this.celkHodiny = new Double(0);
        this.celkDoprava = 0;
        for (int i = 0; i < this.getPocetDnu(); i++) {
            dny.add(new DenEvidence());
        }
        this.den = new Den(mesic-1, rok);
    }

    private Boolean jePrestupny(int i) {
        int prok = i;
        GregorianCalendar cal = new GregorianCalendar();
        if (cal.isLeapYear(prok)) {
            return true;
        } else {
            return false;
        }
    }

    public Integer getPocetDnu() {
        if (this.mesic == 2 && this.jePrestupny(this.rok)) {
            return 29;
        } else {
            return dnu[this.mesic - 1];
        }
    }

    public void addDenniZaznam(int den, Time zacatek, Time konec, String typ, String klient, String ukon, int doprava, Boolean prestavka, String nepritomnost) {
        dny.get(den).addZaznam(zacatek, konec, typ, klient, ukon, doprava, prestavka, nepritomnost);
        this.update();

    }

    public void removeDenniZaznam(int den, int polozka) {
        dny.get(den).removeZaznam(polozka);
        this.update();
    }

    public void editDenniZaznam(int den, int polozka, Map<Integer, Object> prop) {
        this.dny.get(den).editZaznam(polozka, prop);
        this.update();
    }

    private void update() {
        this.celkDoprava = 0;
        this.celkHodiny = 0.0;
        this.doprava.clear();
        this.hodiny.clear();
        for (Iterator<DenEvidence> it = dny.iterator(); it.hasNext();) {
            DenEvidence de = it.next();
            Map<String, Double> dh = de.getHodiny();
            for (Iterator<String> it1 = dh.keySet().iterator(); it1.hasNext();) {
                String key = it1.next();
                if (this.hodiny.containsKey(key)) {
                    double pom = this.hodiny.get(key);
                    pom += dh.get(key);
                    this.hodiny.put(key, pom);
                } else {
                    this.hodiny.put(key, dh.get(key));
                }
            }
            this.celkHodiny += de.getCelkHodiny();
            Map<String, Integer> dd = de.getDoprava();
            for (Iterator<String> it1 = dd.keySet().iterator(); it1.hasNext();) {
                String key = it1.next();
                if (this.doprava.containsKey(key)) {
                    int pom = this.doprava.get(key);
                    pom += dd.get(key);
                    this.doprava.put(key, pom);
                } else {
                    this.doprava.put(key, dd.get(key));
                }
            }
            this.celkDoprava += de.getCelkDoprava();
        }
    }

    public List<List<Object>> getDay(int den) {
        List<List<Object>> pom = this.dny.get(den).getPolozky();
        for (int i = 0; i < pom.size(); i++) {
            List<Object> list = pom.get(i);
            list.add(0, "" + (den + 1) + "." + this.mesic);
        }
        return Collections.unmodifiableList(pom);
    }

    public Boolean isPrestavka(int den) {
        return this.dny.get(den).isPrestavka();
    }

    public Time getPosledni(int den) {
        return this.dny.get(den).getPosledni();
    }

    @Override
    public String toString() {
        return "Mesic_Evidence_" + rok + "_" + mesic;
    }

    public String getStringNazev() {
        return mesice[this.mesic - 1];
    }

    public void export(HSSFWorkbook wb) {
        HSSFCellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setColor(Font.COLOR_RED);
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        font.setFontName("Calibri");
        font.setFontHeightInPoints((short)12);
        style.setFont(font);
        style.setBorderLeft(CellStyle.BORDER_MEDIUM);
        style.setBorderRight(CellStyle.BORDER_MEDIUM);
        style.setAlignment(CellStyle.ALIGN_CENTER);
        int pocRadek = 29;
        HSSFSheet sheet0 = wb.getSheetAt(0);
        sheet0.getRow(3).getCell(8).setCellValue(this.getStringNazev()+" "+this.rok);
        for (int i = 0; i < TypSluzby.pocetSluzeb(); i++) {
            String sluzba = TypSluzby.getSluzba(i);
            if (this.hodiny.containsKey(sluzba)) {
                sheet0.getRow(pocRadek + i).getCell(1).setCellValue(this.hodiny.get(TypSluzby.getSluzba(i)));
            }
            if (this.doprava.containsKey(sluzba)) {
                sheet0.getRow(pocRadek + i).getCell(2).setCellValue(this.doprava.get(TypSluzby.getSluzba(i)));
            }
        }
        sheet0.getRow(38).getCell(1).setCellValue(this.celkHodiny);
        sheet0.getRow(38).getCell(2).setCellValue(this.celkDoprava);
        HSSFSheet sheet1 = wb.getSheetAt(1);
        int row = 1;
        for (int i = 0; i < dny.size(); i++) {
            HSSFCell cell = sheet1.getRow(row).getCell(0);
            cell.setCellValue("" + (i + 1) + "." + this.mesic);
            if (this.den.isweekend(i+1)){
                cell.setCellStyle(style);
            }
            DenEvidence lden = dny.get(i);
            row = lden.export(sheet1, row);
            if (row == 53 || row == 106) {
                row++;
            }
        }
    }
}
